namespace DotNetNuke.Security.Permissions
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Security;
    using System;
    using System.Collections;

    public class TabPermissionController
    {
        public int AddTabPermission(TabPermissionInfo objTabPermission)
        {
            return DataProvider.Instance().AddTabPermission(objTabPermission.TabID, objTabPermission.PermissionID, objTabPermission.RoleID, objTabPermission.AllowAccess);
        }

        public void DeleteTabPermission(int TabPermissionID)
        {
            DataProvider.Instance().DeleteTabPermission(TabPermissionID);
        }

        public void DeleteTabPermissionsByTabID(int TabID)
        {
            DataProvider.Instance().DeleteTabPermissionsByTabID(TabID);
        }

        public ArrayList GetTabPermissionsByPortal(int PortalID)
        {
            TabPermissionController objTabPermissionController = new TabPermissionController();
            ArrayList arrTabPermissions = null;
            if (Globals.PerformanceSetting != Globals.PerformanceSettings.NoCaching)
            {
                arrTabPermissions = (ArrayList) DataCache.GetCache("GetTabPermissionsByPortal" + PortalID.ToString());
            }
            if (arrTabPermissions == null)
            {
                arrTabPermissions = CBO.FillCollection(DataProvider.Instance().GetTabPermissionsByPortal(PortalID), typeof(TabPermissionInfo));
                if (Globals.PerformanceSetting != Globals.PerformanceSettings.NoCaching)
                {
                    DataCache.SetCache("GetTabPermissionsByPortal" + PortalID.ToString(), arrTabPermissions);
                }
            }
            return arrTabPermissions;
        }

        public ArrayList GetTabPermissionsByTabID(int TabID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetTabPermissionsByTabID(TabID, -1), typeof(TabPermissionInfo));
        }

        public TabPermissionCollection GetTabPermissionsByTabID(ArrayList arrTabPermissions, int TabID)
        {
            TabPermissionCollection p = new TabPermissionCollection();
            int refIntHelperL0 = arrTabPermissions.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                TabPermissionInfo objTabPermission = (TabPermissionInfo) arrTabPermissions[i];
                if (objTabPermission.TabID == TabID)
                {
                    p.Add(objTabPermission);
                }
            }
            return p;
        }

        public string GetTabPermissionsByTabID(ArrayList arrTabPermissions, int TabID, string PermissionKey)
        {
            string strRoles = ";";
            int refIntHelperL0 = arrTabPermissions.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                TabPermissionInfo objTabPermission = (TabPermissionInfo) arrTabPermissions[i];
                if (((((objTabPermission.TabID == TabID) && objTabPermission.AllowAccess) && (objTabPermission.PermissionKey == PermissionKey)) ? 1 : 0) != 0)
                {
                    strRoles = strRoles + objTabPermission.RoleName + ";";
                }
            }
            return strRoles;
        }

        public TabPermissionCollection GetTabPermissionsCollectionByTabID(int TabID)
        {
            IList objTabPermissionCollection = new TabPermissionCollection();
            CBO.FillCollection(DataProvider.Instance().GetTabPermissionsByTabID(TabID, -1), typeof(TabPermissionInfo), ref objTabPermissionCollection);
            return (TabPermissionCollection) objTabPermissionCollection;
        }

        public TabPermissionCollection GetTabPermissionsCollectionByTabID(ArrayList arrTabPermissions, int TabID)
        {
            return new TabPermissionCollection(arrTabPermissions, TabID);
        }

        public static bool HasTabPermission(string PermissionKey)
        {
            TabPermissionCollection t = PortalController.GetCurrentPortalSettings().ActiveTab.TabPermissions;
            int refIntHelperL0 = t.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                TabPermissionInfo tp = t[i];
                if ((((tp.PermissionKey == PermissionKey) && PortalSecurity.IsInRoles(tp.RoleName)) ? 1 : 0) != 0)
                {
                    return true;
                }
            }
            return false;
        }

        public static bool HasTabPermission(TabPermissionCollection objTabPermissions, string PermissionKey)
        {
            PortalSettings _PortalSettings = PortalController.GetCurrentPortalSettings();
            TabPermissionCollection t = objTabPermissions;
            int refIntHelperL0 = t.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                TabPermissionInfo tp = t[i];
                if ((((tp.PermissionKey == PermissionKey) && PortalSecurity.IsInRoles(tp.RoleName)) ? 1 : 0) != 0)
                {
                    return true;
                }
            }
            return false;
        }

        public void UpdateTabPermission(TabPermissionInfo objTabPermission)
        {
            DataProvider.Instance().UpdateTabPermission(objTabPermission.TabPermissionID, objTabPermission.TabID, objTabPermission.PermissionID, objTabPermission.RoleID, objTabPermission.AllowAccess);
        }
    }
}

